package com.lz.yz.reward.service.impl;

import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.core.AbstractService;
import com.lz.yz.order.enumeration.OrderStatus;
import com.lz.yz.order.enumeration.OrderType;
import com.lz.yz.outing.service.WechatService;
import com.lz.yz.reward.bean.ComplaintsBean;
import com.lz.yz.reward.bean.RewardBean;
import com.lz.yz.reward.dao.ComplaintsMapper;
import com.lz.yz.reward.dao.RewardMapper;
import com.lz.yz.reward.entity.Complaints;
import com.lz.yz.reward.entity.Reward;
import com.lz.yz.reward.enumeration.ComplaintStatus;
import com.lz.yz.reward.enumeration.RewardStatus;
import com.lz.yz.reward.model.ComplaintsModel;
import com.lz.yz.reward.param.RewardParam;
import com.lz.yz.reward.service.ComplaintsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author gaoran
 * @create 2017-12-18 11:11
 **/
@Service
@Transactional
public class ComplaintsServiceImpl extends AbstractService<Complaints> implements ComplaintsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ComplaintsMapper complaintsMapper;

    @Autowired
    private RewardMapper rewardMapper;

    @Autowired
    WechatService wechatService;

    @Override
    public ReturnValue runComplaint(ComplaintsModel complaintsModel) {
        ComplaintStatus complaintType = ComplaintStatus.getComplaintTypeByIndex(complaintsModel.getStatus());
        Complaints complaints = complaintsMapper.selectByPrimaryKey(complaintsModel.getComplaintsId());
        Reward reward = rewardMapper.selectByPrimaryKey(complaints.getRewardId());
        switch (complaintType) {
            case OVERRULE: //投诉驳回
                complaints.setStatus(ComplaintStatus.OVERRULE.getStatusValue());
                reward.setStatus(RewardStatus.ANSWERED.getStatusValue());
                rewardMapper.updateByPrimaryKey(reward);
                break;
            case PASS: //投诉确认
                complaints.setStatus(ComplaintStatus.PASS.getStatusValue());
                try {
                    //退款操作
                    int rid = reward.getId();
                    String sponsorOrderNo = rewardMapper.selectByPrimaryKey(rid).getOrderNo(); //发起人订单
                    if (sponsorOrderNo != null) {
                        //发布人退款
                        ReturnValue returnValue = wechatService.wechatPayRefund(sponsorOrderNo);
                        if (returnValue.getCode() == 200) {
                            RewardParam param = new RewardParam();
                            param.setOrderType(OrderType.REWARD.getStatusValue());
                            param.setOrderStatus(OrderStatus.COMPLETE.getStatusValue());
                            param.setRewardId(rid);
                            logger.info("sponsorWechatPayRefund:" + rid + ",orderNo:" + sponsorOrderNo);
                            //所有参与过众筹的人退款
                            rewardMapper.queryNotFoundRewardOrder(param).forEach(order -> {
                                logger.info("rewardWechatPayRefund:" + rid + ",orderNo:" + order.getOrderNo());
                                wechatService.wechatPayRefund(order.getOrderNo());
                            });
                        }
                    }
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }
                break;
        }
        complaintsMapper.updateByPrimaryKeySelective(complaints);
        return ReturnValue.generateTrueMsgReturnValue("操作成功");
    }


    @Override
    public List<ComplaintsBean> queryComplaintList() {
        List<ComplaintsBean> list = complaintsMapper.queryComplaintsList();
        return list;
    }
}
